package com.jbh.fourclibrary.fragment;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;


import com.jbh.fourclibrary.R;
import com.jbh.fourclibrary.activity.PlayMp4Activity;
import com.jbh.fourclibrary.util.Util;
import com.jbh.fourclibrary.view.MaterialVideoView;

import java.io.IOException;

public class CutFragment extends Fragment  implements RadioGroup.OnCheckedChangeListener{
    public MaterialVideoView mSurfaceView;
    private RadioGroup gCut;
    ImageView im_1,im_2,im_3;
    public CutFragment() {
    }
    public static CutFragment newInstance() {
        CutFragment fragment = new CutFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_cut, container, false);
        initView(view);
        return view;
    }



    /**
     * @param view
     */
    private void initView(View view) {
        im_1 = view.findViewById(R.id.im_1);
        im_2 = view.findViewById(R.id.im_2);
        im_3 = view.findViewById(R.id.im_3);
        mSurfaceView=view.findViewById(R.id.mSurfaceView);
        mSurfaceView.setSupportLoading(false);
        gCut=view.findViewById(R.id.gCut);
        gCut.setOnCheckedChangeListener(this);
        view.findViewById(R.id.tv_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), PlayMp4Activity.class).putExtra("av_name","av_cut"));
            }
        });
        initPlayAv("av_cut_3");
        measure(view);
    }


    public void check(int t){
        im_1.setImageResource(0);
        im_2.setImageResource(0);
        im_3.setImageResource(0);
        switch (t){
            case 0:
                im_1.setImageResource(R.mipmap.img_check_cut_bg);
                break;
            case 1:
                im_2.setImageResource(R.mipmap.img_check_cut_bg);
                break;
            case 2:
                im_3.setImageResource(R.mipmap.img_check_cut_bg);
                break;
        }
    }

    private void measure(View view) {
        View cut_view = view.findViewById(R.id.cut_view);
        ViewGroup.LayoutParams param = cut_view.getLayoutParams();
        param.height = Util.getHeight(getActivity())
                - Util.getStatusBarHeight(getActivity())
                - Util.dpToPx(getActivity(),90);

        ViewGroup.LayoutParams p = mSurfaceView.getLayoutParams();
        p.height =  param.height * 5/7 ;
        p.width = param.height * 6/7 ;
    }



    @Override
    public void onResume() {
        super.onResume();
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
       String avName = null;
        if (checkedId == R.id.r_ex) {
            avName = "av_cut_3";
            check(0);
        } else if (checkedId == R.id.r_vg) {
            avName = "av_cut_1";
            check(1);
        } else if (checkedId == R.id.r_gd) {
            avName = "av_cut_2";
            check(2);
        }
        initPlayAv(avName);
    }


    /**
     */
    private void initPlayAv(String avName) {
        mSurfaceView.reset();
        mSurfaceView.setLooping(true);
        mSurfaceView.setData("android.resource://"+getActivity().getPackageName()+"/raw/"+avName,MaterialVideoView.VIDEO_TYPE_FILE_PATH);
        mSurfaceView.start();
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSurfaceView .reset();
        mSurfaceView = null;
    }
}
